///////////////////////////////////////////////
///      Product Name: WordQuicker          ///
/// Copyright (c)2007, Marstone Studio      ///
///           Author: marstone              ///
///        Web Site: marstone.net           ///
///      Mail/MSN: marstone@163.com         ///
///         Created: 2007.09.25             ///
///      Last Modified: 2007.09.27          ///
///////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace Studio.Windows.WordQuicker
{
    public class WQApplication
    {

        /// <summary>
        /// The Word Application
        /// </summary>
        private ApplicationClass WordApplication = null;


        /// <summary>
        /// Construct the ApplicationClass Instance
        /// </summary>
        public WQApplication()
        {
            WordApplication = new ApplicationClass();

        }


        /// <summary>
        /// Construct the ApplicationClass Instance
        /// </summary>
        public WQApplication(bool Visable, string Caption)
        {
            WordApplication = new ApplicationClass();
            WordApplication.Caption = Caption;
            WordApplication.Visible = Visable;
            WordApplication.Options.CheckSpellingAsYouType = false;
            WordApplication.Options.CheckGrammarAsYouType = false;
        }



        public WQDocument OpenDocument(string FileName)
        {

            Document WordDoc = null;

            Object filename = FileName;
            Object ConfirmConversions = false;
            Object ReadOnly = false;
            Object AddToRecentFiles = false;

            Object PasswordDocument = System.Type.Missing;
            Object PasswordTemplate = System.Type.Missing;
            Object Revert = System.Type.Missing;
            Object WritePasswordDocument = System.Type.Missing;
            Object WritePasswordTemplate = System.Type.Missing;
            Object Format = System.Type.Missing;
            Object Encoding = System.Type.Missing;
            Object Visible = System.Type.Missing;
            Object OpenAndRepair = System.Type.Missing;
            Object DocumentDirection = System.Type.Missing;
            Object NoEncodingDialog = System.Type.Missing;
            Object XMLTransform = System.Type.Missing;

            try
            {
                WordDoc = WordApplication.Documents.Open(ref filename, ref ConfirmConversions,
                        ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
                        ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
                        ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
                        ref NoEncodingDialog, ref XMLTransform);

                return new WQDocument(WordDoc);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }

        }

        /// <summary>
        /// Close & Save without prompt
        /// </summary>
        public void CloseDocument(bool Save)
        {
            // Closes all documents: saves with no prompt
            Object saveChanges = Save ? WdSaveOptions.wdSaveChanges : WdSaveOptions.wdDoNotSaveChanges;
            Object originalFormat = Type.Missing;
            Object routeDocument = Type.Missing;
            WordApplication.Documents.Close(ref saveChanges,
              ref originalFormat, ref routeDocument);    
        }







        public void Quit(bool Save)
        {
            Object saveChanges = Save ? WdSaveOptions.wdSaveChanges : WdSaveOptions.wdDoNotSaveChanges;
            Object originalFormat = Type.Missing;
            Object routeDocument = Type.Missing;
            WordApplication.Documents.Close(ref saveChanges,
              ref originalFormat, ref routeDocument);
        }

        /// <summary>
        /// Quit without Save
        /// </summary>
        public void Quit()
        {
            Quit(false);
        }
    }
}
